package com.ylq.controller;

import com.ylq.entity.*;
import com.ylq.secret.MD5Utils;
import com.ylq.service.UserRolesService;
import com.ylq.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@Api(description = "【用户】相关的增删改查操作")
@RequestMapping("/api")
public class UsersController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private UserRolesService urService;
    Users users = new Users();


    @ApiOperation("用户名和密码进行登陆")
    @PostMapping("/login")
    public Object login(HttpSession session, @ApiParam("登陆用户名")@RequestParam("un") String un, @ApiParam("登陆密码")@RequestParam("pwd") String pwd, @ApiParam("数据库密钥")@RequestParam("token") String token) {
        String getToken = (String) session.getAttribute("token");
        System.out.println(users.getIsLockout());
        if(getToken.equals(token)){
            //usersService.geytIsLock()
            Users users = usersService.login(un, pwd);
            if(users.getIsLockout().equals("是")){
                return new Result("", "用户已锁定", false);
            }
            List<Roles> list = usersService.getRolesByUserName(un);
            String[] array = new String[list.size()];

            for(int i = 0 ;i< list.size(); i++){
                array[i] = list.get(i).getRoleNames();
            }

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("uid",users.getId());
            map.put("roleNames",array);

            if (users != null) {
                return new Result(map,"登陆成功" , true);
            } else {
                return new Result("", "登录失败", false);
            }
            }else{
                return new Result("", "账户已锁定", false);
            }
    }
    @ApiOperation("创建用户")
    @PostMapping("/CreateUser")
    public Object createUser(HttpSession session,@ApiParam("登陆名")@RequestParam("loginName")String loginName, @ApiParam("密码")@RequestParam("pwd") String pwd,@ApiParam("邮箱")@RequestParam("email") String email,@ApiParam("手机号码")@RequestParam("mtel") String mtel,@ApiParam("数据库密钥")@RequestParam("token") String token){

        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            Users user = null;
            user = usersService.getUserByName(loginName);
            if (user != null && user.getLoginName().equals(loginName)) {
                return new Result("", "用户名已存在", false);
            }
            users.setLoginName(loginName);
            users.setPassword(pwd);
            users.setProtectEmail(email);
            users.setProtectMtel(mtel);
            int i = usersService.createUser(users);
            if (i>0){
                return new Result(users.getId(),"添加成功",true);
            }else {
                return new Result("","添加失败",false);
            }
        }else {
            return new Result("","数据库密钥错误",false);
        }
    }
    @ApiOperation("更新用户")
    @PostMapping("/UpdateUser")
    public Object updateUser(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token, @ApiParam("登录名(登录名是唯一的像主键一样作为修改的where条件)")@RequestParam("loginName")String loginName,@ApiParam("电子邮箱")@RequestParam("email") String email,@ApiParam("电话")@RequestParam("mtel") String mtel){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            users.setLoginName(loginName);
            users.setProtectEmail(email);
            users.setProtectMtel(mtel);
            int i = usersService.updateUser(users);
            if (i>0){
                return new Result("","修改成功",true);
            }else {
                return new Result("","修改失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("重置密码")
    @PostMapping("/ResetPassword")
    public Object resetPassword(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("用户名")@RequestParam("un") String un){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            users.setLoginName(un);
            int i = usersService.resetPassword(users);
            if (i>0){
                return new Result("","重置成功",true);
            }else {
                return new Result("","重置失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("锁定用户")
    @PostMapping("/LockUser")
    public Object lockUser(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("用户名")@RequestParam("un") String un){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            users.setLoginName(un);
            int i = usersService.lockUser(users);
            if (i>0){
                return new Result("","锁定成功",true);
            }else {
                return new Result("","锁定失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
    @ApiOperation("解锁用户")
    @PostMapping("/UnLockUser")
    public Object unLockUser(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("用户名")@RequestParam("un") String un){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            users.setLoginName(un);
            int i = usersService.unLockUser(users);
            if (i>0){
                return new Result("","解锁成功",true);
            }else {
                return new Result("","解锁失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("删除用户")
    @PostMapping("/DeleteUser")
    public Object deleteUser(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("用户ID")@RequestParam("uid") String uid){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            users.setId(uid);
            int i = usersService.deleteUser(uid);
            urService.deleteUr(uid);
            if (i>0){
                return new Result(users.getId(),"删除成功",true);
            }else{
                return new Result("","删除失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("分页的方式获取指定条件的用户信息")
    @PostMapping("/GetUsersLayUI")
    public Object getUsersLayUI(HttpSession session,@ApiParam("用户名")@RequestParam(value = "userName",required=false)String userName,
                              @ApiParam("开始时间")@RequestParam(value = "beginDate",required=false)String beginDate,@ApiParam("结束时间")@RequestParam(value = "endDate",required=false)String endDate,
                              @ApiParam("是否锁定")@RequestParam(value = "isLock",required=false)String isLock,@ApiParam("每页显示数据条数")@RequestParam("limit")int limit,
                              @ApiParam("页码")@RequestParam("page")int page, @ApiParam("数据库秘钥")@RequestParam("token")String token) {
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
//System.out.println(userName);
            UsersDto dto = new UsersDto();
            //System.out.println(userName);
            dto.setUserName(userName);
            dto.setBeginDate(beginDate);
            dto.setEndDate(endDate);
            dto.setIsLock(isLock);
            PageVo pageVo = new PageVo();
            pageVo.setPage(page);
            pageVo.setPageSize(limit);
            dto.setPageVo(pageVo);
            //System.out.println(dto);
            //System.out.println(pageVo);
            List<Users> list = usersService.getUsersLayUI(dto);
            //int i = usersService.countAllUsers(dto);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code",0);
            map.put("msg","");
            map.put("count",list.size());
            map.put("data",list);
            return map;
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }

    }
